<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
 <%@ page import="java.util.*" %>
 <%@ page import="org.json.simple.JSONObject" %>
 <%@ page import="org.json.simple.parser.JSONParser" %>
 <%@ page import="org.json.simple.JSONArray" %>
 <%@ page import="com.netcracker.studportal.server.facade.*" %> 
 <%@ page import="org.apache.commons.lang.StringEscapeUtils"
 		  import="java.net.*"
 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Portal - Teacher View</title>
<link href="style/css/cssShedule.css" rel="stylesheet" type="text/css">
</head>
<body>

<div class="main">
	<div class="wrapper">
	
	
	<div id="panel-navigation">
			<div class="m">
			<ul id="navigationMenu">
				<li>
					<a class="home" href="index.jsp">
						<span>На главную</span>
					</a>
				</li>
				<li>
					<a class="about" href="logout.jsp">
						<span>Выйти</span>
					</a>
				</li>
				<li>
					<a class="contact" href="#">
						<span>Контакты</span>
					</a>
				</li>
			</ul>
			<div class="clear"></div>
			</div>
		</div>
	
		<div class="oval11">
			 <a href = "<% out.print("professor.jsp?action=schedule"); %>" >
			<div class="content">Расписание экзаменов</div>
			</a>
		</div>
        
        <div class="oval21">
       	   <a href = "<% out.print("professor.jsp?action=exam_res"); %>" >
   	    <div class="content">Результаты экзаменов</div>
   	    </a>
        </div>
        
		<div class="oval31">
       	  <a href="<% out.print("professor.jsp?action=materials"); %>">
       	  <div class="content">Дополнительные материалы</div>
       	  </a>
        </div>
        
        <div class="cont">
        </div>
		
		<div class="cont-body"> 
		   <div class="general-content">
<%
	if ((session.getAttribute("username") == null) || ("").equals(session.getAttribute("username")))
	{	
		response.sendRedirect("login.jsp");
	}

	request.setCharacterEncoding("UTF-8");
	
	String prof_id = session.getAttribute("user_id").toString();
 	String action = request.getParameter("action");
	
	if ("set_res".equals(action))
	{
		String exam_id = request.getParameter("ex_id");
		
		FacadeProfessor fa = new FacadeProfessor();
		JSONObject obj=new JSONObject();
		obj.put("exam_id",exam_id);

		String param = obj.toString();
		param = fa.getStudentResultsByExamID(param);
		
		JSONParser parser=new JSONParser(); 
		Object ob=parser.parse(param);
		JSONArray array=(JSONArray)ob;
		
		Iterator iter=array.iterator();
		while(iter.hasNext())
		{
			JSONObject params =new JSONObject();
			JSONObject data = (JSONObject) iter.next();
			String student = data.get("student_id").toString();
			String mark = request.getParameter("mark_"+student);
			params.put("exam_id",exam_id);
			params.put("student_id",student);
			params.put("mark",mark);
			params.put("note","");
			fa.setExamResultByStudent(params.toString());
		}
		out.print("<p class='liter'>Изменения сохранены</p>");
	}
	
	if ("current_exam".equals(action))
	{
		String exam_id = request.getParameter("ex_id");
		String subjectName = request.getParameter("sub_name");
		
		FacadeProfessor fa = new FacadeProfessor();
		JSONObject obj=new JSONObject();
		obj.put("exam_id",exam_id);

		String param = obj.toString();
		param = fa.getStudentResultsByExamID(param);

		JSONParser parser=new JSONParser(); 
		Object ob=parser.parse(param);
		if (ob instanceof JSONArray) {
		JSONArray array=(JSONArray)ob;
		Iterator iter=array.iterator();
		
		out.print("<p class='liter'>Результаты сдачи экзамена по предмету \""+subjectName+"\" </p>");
		out.print("<p class='title-mark'><span class='stud-name'>Студент</span><span class='course'>Курс</span><span class='group'>Группа</span><span class='mark'>Оценка</span></p>");
		out.print("<form action='professor.jsp?action=set_res&ex_id="+exam_id+"' method=\"post\">");
		out.print("<ul class='student-list'>");
		
		while(iter.hasNext()){
		JSONObject data = (JSONObject) iter.next();
	    String stud_name = data.get("student_name").toString();
	    String mark = data.get("mark").toString();
	    String course = data.get("course").toString();
	    String groupNum = data.get("group_num").toString();
		
	    out.print("<li>");
		
		out.print("<span class='name'>"+stud_name+"</span>");
        out.print("<span class='course-num'>" + course + "</span>");
        out.print("<span class='group-num'>" + groupNum + "</span>");
        out.print("<input class='mark' type='text' name='mark_"+data.get("student_id").toString()+"' value='"+mark+"'></input>");  
		out.print("</li>");
		}
		out.print("</ul>");
		out.print("<div class='send_button'><input type='submit' value='Сохранить'></div>");
		out.print("</form>");
		}
		else {
			JSONObject jsonObj = (JSONObject) ob;
			String error = jsonObj.get("error").toString();
			String stacktrace = jsonObj.get("stacktrace").toString();
			
			out.print("Has occurred exception:\n" + error + "\n" + stacktrace);
		}
	}
	
	if ("exam_res".equals(action))
	{
		FacadeProfessor fa = new FacadeProfessor();
		JSONObject obj=new JSONObject();
		obj.put("prof_id",prof_id);

		String param = obj.toString();
		param = fa.getSheduleExamsByProfessor(param);
	
		JSONParser parser=new JSONParser(); 
		Object ob=parser.parse(param);
		if (ob instanceof JSONArray) {
		JSONArray array=(JSONArray)ob;
		
		Iterator iter=array.iterator();
		
		while(iter.hasNext()){
		JSONObject data = (JSONObject) iter.next();
	    
	    String s_name = data.get("subject_name").toString();
	    String e_date = data.get("exam_date").toString();
	    String d_type = data.get("type").toString();
		String exam_id = data.get("exam_id").toString();
	    out.print("<a href='professor.jsp?action=current_exam&ex_id="+exam_id+"&sub_name="+URLEncoder.encode(s_name, "UTF-8")+"'><div class='stick'>");
		
		out.print("<p class='title'>Предмет:</p>");
		out.print("<p class='txt'>"+s_name+"</p>");
		out.print("<p class='title'>Дата:</p>");
		out.print("<p class='txt'>" + e_date+"</p>");
		out.print("<p class='title'>Тип:</p>");
		out.print("<p class='txt'>"+d_type+"</p>");
		
		out.print("</div></a>");
	
		}
		}else {
			JSONObject jsonObj = (JSONObject) ob;
			String error = jsonObj.get("error").toString();
			String stacktrace = jsonObj.get("stacktrace").toString();
			
			out.print("Has occurred exception:\n" + error + "\n" + stacktrace);
		}
	}
	
	if ("schedule".equals(action))
	{
		FacadeProfessor fa = new FacadeProfessor();
		JSONObject obj=new JSONObject();
		obj.put("prof_id",prof_id);

		String param = obj.toString();
		param = fa.getSheduleExamsByProfessor(param);
	
		JSONParser parser=new JSONParser(); 
		Object ob=parser.parse(param);
		if (ob instanceof JSONArray) {
		JSONArray array=(JSONArray)ob;
		
		Iterator iter=array.iterator();
		
		while(iter.hasNext()){
		JSONObject data = (JSONObject) iter.next();
	    
	    String s_name = data.get("subject_name").toString();
	    String e_date = data.get("exam_date").toString();
	    String d_type = data.get("type").toString();
	    
	    out.print("<div class='stick'>");
		
		out.print("<p class='title'>Предмет:</p>");
		out.print("<p class='txt'>"+s_name+"</p>");
		out.print("<p class='title'>Дата:</p>");
		out.print("<p class='txt'>" + e_date+"</p>");
		out.print("<p class='title'>Тип:</p>");
		out.print("<p class='txt'>"+d_type+"</p>");
		
		out.print("</div>");
	
		}
		}else {
			JSONObject jsonObj = (JSONObject) ob;
			String error = jsonObj.get("error").toString();
			String stacktrace = jsonObj.get("stacktrace").toString();
			
			out.print("Has occurred exception:\n" + error + "\n" + stacktrace);
		}
	}
	
	if ("materials".equals(action)) {
		FacadeProfessor fa = new FacadeProfessor();
		
		Object examId = request.getParameter("ex_id");
		if (examId != null) 
		{
			/* Show materials for the selected exam */
			JSONObject inputParams = new JSONObject();
			inputParams.put("exam_id", examId.toString());
			String result = fa.getMaterialsByExam(inputParams.toJSONString());
			
			JSONParser parser = new JSONParser();
			Object obj = parser.parse(result);
			if (obj instanceof JSONArray) {
				JSONArray jsonArray = (JSONArray) obj;
				Iterator<JSONObject> i = jsonArray.iterator();
				while (i.hasNext()) {
					JSONObject data = (JSONObject) i.next();
			    	String file_name = URLDecoder.decode(data.get("material_name").toString(), "UTF-8");
			    	String materialId = data.get("material_id").toString();

			    	String link2Material = "download.jsp?material_id=" + materialId + "&user_type=2";
					out.print("<li><a href='" + link2Material + "' style=\"color:white\">"+file_name+"</a></li>");
				}
%>
<br /><br />
<form action="upload.jsp" method="post" enctype="multipart/form-data">
	<input type="file" name="file" size="50" /> <br/>
	Описание материала:<br /> 
	<input type="text" name="material_name" size="50" /> <br />
	<input type="submit" name="upload" value="Загрузить" /> <br />
	<input type="hidden" name="exam_id" value=<%=examId %> />
	<input type="hidden" name="return" value=<%=request.getRequestURL().append('?').append(request.getQueryString()).toString() %> />
</form>
<%
			} else {
				JSONObject jsonObj = (JSONObject) obj;
				out.print("Ошибка: " + jsonObj.get("error"));
			}
		} else {
			/* Show list of exams */
			JSONObject inputParams = new JSONObject();
			inputParams.put("prof_id", prof_id);
			String result = fa.getSheduleExamsByProfessor(inputParams.toJSONString());
			
			JSONParser parser = new JSONParser();
			Object obj = parser.parse(result);
			if (obj instanceof JSONArray) {
				JSONArray jsonArray = (JSONArray) obj;
				Iterator<JSONObject> i = jsonArray.iterator();
				while (i.hasNext()) {
					JSONObject data = (JSONObject) i.next();
				    String s_name = data.get("subject_name").toString();
				    String e_date = data.get("exam_date").toString();
				    String d_type = data.get("type").toString();
					String exam_id = data.get("exam_id").toString();
				    out.print("<a href='professor.jsp?action=materials&ex_id="+exam_id+"'><div class='stick'>");
					
					out.print("<p class='title'>Предмет:</p>");
					out.print("<p class='txt'>"+s_name+"</p>");
					out.print("<p class='title'>Дата:</p>");
					out.print("<p class='txt'>" + e_date+"</p>");
					out.print("<p class='title'>Тип:</p>");
					out.print("<p class='txt'>"+d_type+"</p>");
					
					out.print("</div></a>");
				}
			}
		}
	}
 %>
				</div>
			</div>
        </div>
  </div>

  

</body>
</html>